$(function () {
    
    /*显示用户信息*/
    $('#customer_select').on('change',function(){
        var cus_id = $(this).val();
        $('#customer_id').val(cus_id);
        var url = '?m=index-Customer&do=getInfoById';
        $.getJSON(url,{'id':cus_id},function(data){
            if(data.code == 200){
                $('#address').text(data.ext.address);
                $('#name').text(data.ext.name);;
                $('#mobile').text(data.ext.mobile);
                $('#balance').text(data.ext.balance);;
                $('#discount').text(data.ext.discount);
                $('#customer_info').removeClass('hide');
                /*显示快递费*/
                $('#exp_charge').removeClass('hide');
            }
        })
    });
    
    /*提交订单*/
    $('.submit').on('click',function(){
        //数据验证
        var che =  check();
        if (che == false){
            return false;
        }else{
            intInput();
        }
        
    });
});

/*获取输入信息*/
function intInput() {
    /*用户信息*/
    var para = new Object();
    para.cus_id = $('#customer_id').val();
    para.mobile = $('#mobile').text();
    para.cus_name = $('#name').text();
    para.cus_address = $('#address').text();
    para.discount = $('#discount').text();
    para.origin_price = 0;
    para.count = 0;
    
    /*快递费用确认*/
    var exp_charge = $('#exp_charge_val').val();
    para.exp_charge = exp_charge;
    if(exp_charge<=0){
        alert('确认快递费用为0?');
    }
    
    /*产品信息*/
    var cou = 0;
    para.product = new Array();
    $('input[name="p_count"]').each(function () {
        var val = $(this).val();
        if (val.length != 0  && val!=0) {
            var pro = new Object();
            var p = $(this).parent().prev();
            pro.id = p.attr('ids');
            pro.name = p.attr('name');
            pro.price = p.attr('price');
            pro.count = val;
            para.count += parseInt(pro.count);
            pro.money = pro.price * pro.count;
            para.origin_price += pro.money;
            para.product[cou] = pro;
            cou++;
        }
    });
    if (cou == 0) {
        l_fail('产品不能为空');
        return false;
    } else {
        //确认提示内容
        var markup = "<tr><td>${id}</td><td>${name}</td><td>${price}</td><td >${count}</td><td>${money}</td></tr>"
        $.template("produtList", markup);
        $("#product_list").html('');
        $('#total_count').text('总数:'+para.count);
        $('#total_origin_price').text(para.origin_price);
        $('#order_origin_price').text('会员价:' + para.origin_price);
        var discount = para.discount == 100 ? '无' : para.discount + '折';
        $('#order_discount').text('会员折扣:' + discount);
        $('#order_price').text(((para.origin_price * para.discount) / 100) +'+'+ exp_charge + '='+(((para.origin_price * para.discount) / 100)+parseInt(exp_charge)));
        $.tmpl("produtList", para.product).appendTo("#product_list");
        var add_info_confirm = $('#order_info').html();
        //提交
        saveConfirm(JSON.stringify(para), para.cus_name, add_info_confirm);
    }
}

/*确认提交*/
function saveConfirm(pa,title,message){
    console.log(pa);
    layui.use(['layer'], function () {
        var layer = layui.layer;
        layer.confirm(message,{"title":title+" 新增订单确认","area":'600px'},function (){
            saveOrder(pa)
        },function (){
            layer.close();
        });
    });
}

/*提交订单*/
function saveOrder(pa){
    var url = '?m=index-Order&do=createOrder'
    var data = {
        data: pa
    };
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: data,
        success: function (res) {
            console.log(res);
            if (res.code == 200)
            {
                var red_url = "?m=index-Order&do=getInfoNew&id="+res.ext;
                l_succcess(res.message);
                setTimeout(function(){
                    redir_url(red_url);
                },1000);
            } else
            {
                l_fail(res.message);
            }
        }
    });
}

/*确认删除*/
function delConfirm(id){
    layui.use(['layer'], function () {
        var layer = layui.layer;
        layer.confirm('确认删除订单?',{"title":"删除确认"},function (){
            delOrder(id);
        },function (){
            layer.close();
        });
    });
}

/*删除订单*/
function delOrder(id){
    var url = '?m=index-Order&do=delOrder';
    var data = {
        id: id
    };
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: data,
        success: function (res) {
            console.log(res);
            if (res.code == 200)
            {
                var red_url = location.href;
                l_succcess(res.message);
                setTimeout(function(){
                    redir_url(red_url);
                },1000);
            } else
            {
                l_fail(res.message);
            }
        }
    });
}

/*订单详情*/
function orderInfo(id){
    var red_url = "?m=index-Order&do=getInfo&id="+id;
    redir_url(red_url);
}

/*订单详情新*/
function orderInfoNew(id){
    var red_url = "?m=index-Order&do=getInfoNew&id="+id;
    redir_url(red_url);
}
